package br.com.comanda.util;

import org.json.JSONObject;

import br.com.comanda.vos.Account;
import br.com.comanda.vos.Board;
import br.com.comanda.vos.Item;
import br.com.comanda.vos.Product;

public class VOToJSON {

	public static JSONObject toBoardJSON(Board board) {
		JSONObject boardJSON = new JSONObject();

		try {
			boardJSON.accumulate("id", board.getId());
			boardJSON.accumulate("board", board.getBoard());
			return boardJSON;
		} catch (Exception e) {
			return null;
		}

	}

	public static JSONObject toAccountJSON(Account account) {
		JSONObject accountJSON = new JSONObject();

		try {
			accountJSON.accumulate("id", account.getId());
			accountJSON.accumulate("client", account.getClient());
			accountJSON.accumulate("waiter", account.getWaiter());
			accountJSON.accumulate("board", toBoardJSON(account.getBoard()));
			return accountJSON;
		} catch (Exception e) {
			return null;
		}
	}

	private static Object toProductJSON(Product product) {
		JSONObject productJSON = new JSONObject();
		try {
			productJSON.accumulate("id", product.getId());
			productJSON.accumulate("name", product.getName());
			productJSON.accumulate("description", product.getDescription());
			productJSON.accumulate("price", product.getPrice());
			return productJSON;
		} catch (Exception e) {
			return null;
		}
	}

	public static JSONObject toItemJSON(Item item) {
		JSONObject itemJSON = new JSONObject();

		try {
			itemJSON.accumulate("id", item.getId());
			itemJSON.accumulate("account", toAccountJSON(item.getAccount()));
			itemJSON.accumulate("product", toProductJSON(item.getProduct()));
			itemJSON.accumulate("quantity", item.getQuantity());
			itemJSON.accumulate("total", item.getTotal());
			return itemJSON;
		} catch (Exception e) {
			return null;
		}
	}

}
